package org.ant.MyCalculate;

import java.util.HashMap;

import android.widget.Button;
import android.widget.TextView;

public class AppearNumberTool {
	private Button button;
	private TextView textView;
	private Double firstNumber;
	private Double secondNumber;
	private Boolean flag;
	private String operation;
	
	AppearNumberTool(Button button ,TextView textView ,Double firstNumber ,Double secondNumber ,Boolean flag ,String operation) {
		this.button = button;
		this.textView = textView;
		this.firstNumber = firstNumber;
		this.secondNumber = secondNumber;
		this.flag = flag;
		this.operation = operation;
	}
	
	HashMap<String, Object> dealNumber (){
		HashMap<String,Object> hashMap = new HashMap<String, Object>();
		if(textView.getText().equals("0.0")){
			if(!isOperation(button)&&!isEquOperation(button)){
				textView.setText("");
			}
		}
		if(!isOperation(button)&&!isEquOperation(button)&&(flag==false)){
			String temp1 = "";
			if(button.getText().equals(".")){
				temp1 = button.getText().toString() + "0";
			}
			firstNumber = new Double((textView.getText().toString() + (temp1.equals("")?button.getText():temp1)).replace(" ", ""));
		}
		if((isOperation(button)|| isEquOperation(button))&&(flag==false)){
			operation = button.getText().toString();
			flag = true;
		}
		if(!isOperation(button)&&!isEquOperation(button)&&(flag==true)){
			int index = textView.getText().toString().indexOf(operation);
			int endIndex =textView.getText().toString().length();
			secondNumber = new Double(textView.getText().toString().substring(index+1,endIndex) + button.getText());
		}
		if(isEquOperation(button)){
			EquationTool equationTool = new EquationTool(button, textView ,firstNumber, secondNumber ,operation ,flag);
			return equationTool.dealEquation();
		}
		textView.setText(textView.getText().toString() + button.getText());
		hashMap.put("textView", textView);
		hashMap.put("flag", flag);
		hashMap.put("operation", operation);
		hashMap.put("firstNumber", firstNumber);
		hashMap.put("secondNumber", secondNumber);
		return hashMap;
	}
	
	private boolean isOperation(Button button){
		if(button.getText().equals("+") 
				|| button.getText().equals("-") 
					|| button.getText().equals("*") 
						|| button.getText().equals("/")){
			return true;
		}
		return false;
	}
	private boolean isEquOperation(Button button){
		if(button.getText().equals("=")){
			return true;
		}
		return false;
	}
	
}
